//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormItemDrugPharmGroupsSets.h"
#include "FormItemDrugGenericNames.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBGridEh"
#pragma link "DBGridEhGrouping"
#pragma link "FIBDatabase"
#pragma link "FIBDataSet"
#pragma link "GridsEh"
#pragma link "pFIBDatabase"
#pragma link "pFIBDataSet"
#pragma link "PropFilerEh"
#pragma link "PropStorageEh"
#pragma link "TemplateListEditor"
#pragma link "DBCtrlsEh"
#pragma link "DBLookupEh"
#pragma link "FIBQuery"
#pragma link "pFIBQuery"
#pragma resource "*.dfm"
TItemDrugPharmGroupsSetsForm *ItemDrugPharmGroupsSetsForm;
//---------------------------------------------------------------------------

__fastcall TItemDrugPharmGroupsSetsForm::TItemDrugPharmGroupsSetsForm(TComponent* Owner)
	: TListEditorTemplate(Owner)
{
	GenericNameID = -1;
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::ShowModal(TpFIBTransaction* ParentTransaction, int GenericNameID)
{
	DataSet->Transaction = ParentTransaction;
	SelectedDataSet->Transaction = ParentTransaction;
	GenericNamesDataSet->Transaction = ParentTransaction;
	Query->Transaction = ParentTransaction;
	this->GenericNameID = GenericNameID;
	TListEditorTemplate::ShowModal();
	this->GenericNameID = -1;
	DataSet->Transaction = Transaction;
	SelectedDataSet->Transaction = Transaction;
	GenericNamesDataSet->Transaction = Transaction;
	Query->Transaction = Transaction;
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::ShowModal(int GenericNameID)
{
	this->GenericNameID = GenericNameID;
	TListEditorTemplate::ShowModal();
	this->GenericNameID = -1;
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::FormResize(TObject *Sender)
{
	DBGridEh->Width = (ClientWidth - PanelDivider->Width)/2;
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::FormShow(TObject *Sender)
{
	GenericNamesDataSet->Open();
	MasterDataSet->CreateDataSet();
	MasterDataSet->Append();
	if(GenericNameID >= 0) {
		MasterDataSet->FieldByName("GENERIC_NAME_ID")->AsInteger = GenericNameID;
		DBLookupComboboxEh->Enabled = false;
	}
	else {
		MasterDataSet->FieldByName("GENERIC_NAME_ID")->AsInteger = GenericNamesDataSet->FieldByName("ID")->AsInteger;
		DBLookupComboboxEh->Enabled = true;
	}
	SelectedDataSet->Open();
	TListEditorTemplate::FormShow(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::AddSpeedButtonClick(TObject *Sender)
{
	if(!DataSet->Bof || !DataSet->Eof) {
		Query->SQL->Text =
			"insert into itemdrug_pharm_groups_sets(generic_name_id, pharm_group_id)"
			"values(" + MasterDataSet->FieldByName("GENERIC_NAME_ID")->AsString + "," + DataSet->FieldByName("ID")->AsString + ")";
		Query->ExecQuery();
		SelectedDataSet->FullRefresh();
		SelectedDataSet->Locate("ID", DataSet->FieldByName("ID")->Value, TLocateOptions());

		DataSet->Next();
		if(DataSet->Eof) DataSet->Prior();
		DataSet->FullRefresh();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DeleteSpeedButtonClick(TObject *Sender)
{
	if(!SelectedDataSet->Bof || !SelectedDataSet->Eof) {
		Query->SQL->Text =
			"delete from itemdrug_pharm_groups_sets "
			"where generic_name_id=" + MasterDataSet->FieldByName("GENERIC_NAME_ID")->AsString + " and pharm_group_id=" + SelectedDataSet->FieldByName("ID")->AsString;
		Query->ExecQuery();
		DataSet->FullRefresh();
		DataSet->Locate("ID", SelectedDataSet->FieldByName("ID")->Value, TLocateOptions());

		SelectedDataSet->Next();
		if(SelectedDataSet->Eof) SelectedDataSet->Prior();
		SelectedDataSet->FullRefresh();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DeleteAllSpeedButtonClick(TObject *Sender)
{
	Query->SQL->Text =
		"delete from itemdrug_pharm_groups_sets "
		"where generic_name_id=" + MasterDataSet->FieldByName("GENERIC_NAME_ID")->AsString;
	Query->ExecQuery();
	DataSet->FullRefresh();
	SelectedDataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DBGridEhKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if((Key == VK_RETURN && !Shift.Contains(ssCtrl)) || Key == ' ') AddSpeedButtonClick(AddSpeedButton);
	else if(Key == VK_RIGHT) SelectedDBGridEh->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::SelectedDBGridEhKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if((Key == VK_RETURN && !Shift.Contains(ssCtrl)) || Key == ' ') DeleteSpeedButtonClick(DeleteSpeedButton);
	else if(Key == VK_LEFT) DBGridEh->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DBGridEhDblClick(TObject *Sender)
{
	AddSpeedButtonClick(AddSpeedButton);
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::SelectedDBGridEhDblClick(TObject *Sender)
{
	DeleteSpeedButtonClick(DeleteSpeedButton);
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DBLookupComboboxEhChange(TObject *Sender)
{
	if(MasterDataSet->State == dsEdit || MasterDataSet->State == dsInsert) MasterDataSet->Post();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DBLookupComboboxEhEditButtonsEllipsisClick(TObject *Sender,
		  bool &Handled)
{
	if(!ItemDrugGenericNamesForm->Visible) {
		ItemDrugGenericNamesForm->ShowModal();
		GenericNamesDataSet->FullRefresh();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Key == VK_DOWN && Shift.Contains(ssAlt)) DBLookupComboboxEh->DropDown();
	TListEditorTemplate::FormKeyDown(Sender, Key, Shift);
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DBLookupComboboxEhEnter(TObject *Sender)
{
	if(!DBLookupComboboxEh->ListVisible) DBLookupComboboxEh->DropDown();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DBLookupComboboxEhEditButtonsDropDownClick(TObject *Sender, bool &Handled)
{
	if(!DBLookupComboboxEh->ListVisible) DBLookupComboboxEh->DropDown();
}
//---------------------------------------------------------------------------

void __fastcall TItemDrugPharmGroupsSetsForm::DBLookupComboboxEhKeyDown(TObject *Sender,WORD &Key, TShiftState Shift)
{
	if(Key == VK_ESCAPE) DBLookupComboboxEh->SelectAll();
	else if(Key == VK_RETURN) {
		Key = NULL;
		DBLookupComboboxEh->CloseUp(true);
		DBGridEh->SetFocus();
	}
}
//---------------------------------------------------------------------------


